#Copyright (c) 2025, Alibaba Cloud and its affiliates;
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.



import sys
import json
from fce_util.libs.log import g_logger

logging = g_logger.logger
InvalidParams = 'InvalidParams'
ExecFail = 'ExecFail'
UnknownError = 'UnknownError'


class r_stdout:
    def __init__(self):
        self.content = ''
        self.saved_stdout = sys.stdout
    def write(self, out):
        self.content += out
    def to_console(self):
        sys.stdout = self.saved_stdout

    def restore(self):
        sys.stdout = self.saved_stdout
    def set(self):
        sys.stdout = self

    def show(self):
        saved = sys.stdout
        sys.stdout = self.saved_stdout
        print(self.content)
        sys.stdout = saved

class r_stderr:
    def __init__(self):
        self.content = ''
        self.saved_stderr = sys.stderr
    def write(self, out):
        self.content += out
    def to_console(self):
        sys.stderr = self.saved_stderr
#    def to_mute(self):

    def restore(self):
        sys.stderr = self.saved_stderr
    def set(self):
        sys.stderr = self

    def show(self):
        saved = sys.stderr
        sys.stderr = self.saved_stderr
        print(self.content)
        sys.stderr = saved

cli_rdir_stdout = r_stdout()
cli_rdir_stderr = r_stderr()

def to_fce_service(str):
    if cli_rdir_stdout is not None:
        cli_rdir_stdout.to_console()
        print(str)
        cli_rdir_stdout.set()

def fce_res_common(success, **kw):
    stdout_dict = {}

    if success:
        stdout_dict['Success'] = True
        stdout_dict['Message'] = {}
        if 'message' in kw:
            stdout_dict['Message']['Msg'] = kw['message']
        else:
            stdout_dict['Message'] = {}

    else:
        stdout_dict['Success'] = False
        stdout_dict['Message'] = {}
        if 'errcode' in kw:
            stdout_dict['Message']['ErrCode'] = kw['errcode']
        else:
            stdout_dict['Message']['ErrCode'] = None
        if 'errmsg' in kw:
            stdout_dict['Message']['ErrMsg'] = kw['errmsg']
        elif 'hosts' in kw:
            stdout_dict['Message']['Hosts'] = kw['hosts']
        else:
            stdout_dict['Message']['ErrMsg'] = None

    stdout = json.dumps(stdout_dict)
    if success:
        logging.debug('fce_res_common print %s'%stdout)
    else:
        logging.error('fce_res_common print %s'%stdout)
    print(stdout)
#    to_fce_service(stdout)
    return stdout_dict

def fce_res_common_no_print(success, **kw):
    stdout_dict = {}

    if success:
        stdout_dict['Success'] = True
        stdout_dict['Message'] = {}
        if 'message' in kw:
            stdout_dict['Message']['Msg'] = kw['message']
        else:
            stdout_dict['Message'] = {}

    else:
        stdout_dict['Success'] = False
        stdout_dict['Message'] = {}
        if 'errcode' in kw:
            stdout_dict['Message']['ErrCode'] = kw['errcode']
        else:
            stdout_dict['Message']['ErrCode'] = None
        if 'errmsg' in kw:
            stdout_dict['Message']['ErrMsg'] = kw['errmsg']
        else:
            stdout_dict['Message']['ErrMsg'] = None
            

    stdout = json.dumps(stdout_dict)
    if success:
        logging.debug(stdout)
    else:
        logging.error(stdout)
    # print stdout
#    to_fce_service(stdout)
    return stdout_dict
